﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace FSS.UI
{
    public class UIManager : FSS.UnitySingleton<UIManager>
    {
        public const string PrefabPathPrefix = "UIPrefabs/";
        Dictionary<string, GameObject> DicWnds = new Dictionary<string, GameObject>();
        public GameObject RootCanvas;
        public Canvas theCanvas;

        private void Start()
        {
            Init("");
        }
        public void Init(string canvasName)
        {
            if (canvasName.Length > 0)
            {
                RootCanvas = GameObject.Find(canvasName);
            }

            if(RootCanvas!=null)
            {
                theCanvas = RootCanvas.GetComponent<Canvas>();
            }
        }

        public T ShowWnd<T>(string wndName) where T : UIWnd
        {
            GameObject wnd = null;
            T t = null;
            if (DicWnds.ContainsKey(wndName))
            {
                wnd = DicWnds[wndName];
                t = wnd.GetComponent<T>();
                if(!wnd.activeSelf && t!=null)
                {
                    wnd.SetActive(true);
                    t.OnWndShow();
                }
                return t;
            }
            GameObject obj = Resources.Load<GameObject>(PrefabPathPrefix + typeof(T).ToString());
            wnd = GameObject.Instantiate(obj);
            GameObject.Destroy(obj);
            wnd.name = wndName;
            wnd.transform.SetParent(RootCanvas.transform);
            wnd.transform.localPosition = Vector3.zero;
            wnd.transform.localScale = Vector3.one;
            wnd.transform.localRotation = Quaternion.identity;

            RectTransform rectTrans = wnd.GetComponent<RectTransform>();
            rectTrans.anchorMin = Vector2.zero;
            rectTrans.anchorMax = Vector2.one;
            rectTrans.offsetMin = Vector2.zero;
            rectTrans.offsetMax = Vector2.zero;

            Image panelImage = wnd.GetComponent<Image>();
            if(panelImage)
            {
                panelImage.raycastTarget = false;
            }

            t = wnd.AddComponent<T>();
            t.wndName = wndName;
            t.InitUIElements(wnd.transform);
            DicWnds.Add(wndName, wnd);

            t.OnWndCreate();
            if(wnd.activeSelf)
            {
                t.OnWndShow();
            }
            else
            {
                t.OnWndHide();
            }

            return t;
        }

        public GameObject GetWnd(string wndName)
        {
            GameObject wnd = null;
            if (DicWnds.ContainsKey(wndName))
            {
                wnd = DicWnds[wndName];
                return wnd;
            }
            return wnd;
        }

        public void HideWnd(string name, bool destroy = true)
        {
            if (!DicWnds.ContainsKey(name))
                return;
            GameObject go = DicWnds[name];
            UIWnd wndScript = go.GetComponent<UIWnd>();
            if (destroy)
            {
                DicWnds.Remove(name);
                go.SetActive(false);
                wndScript.OnWndHide();
                wndScript.OnWndDestroy();
                GameObject.Destroy(go);
            }
            else
            {
                go.SetActive(false);
                wndScript.OnWndHide();
            }
        }

        public bool IsPointerOverUIObject(Vector2 screenPosition)
        {
            PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
            eventDataCurrentPosition.position = new Vector2(screenPosition.x, screenPosition.y);
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
            return results.Count > 0;
        }

        bool CheckGuiRaycastObjects()
        {
            PointerEventData eventData = new PointerEventData(EventSystem.current);
            eventData.pressPosition = Input.mousePosition;
            eventData.position = Input.mousePosition;
            List<RaycastResult> list = new List<RaycastResult>();
            theCanvas.GetComponent<GraphicRaycaster>().Raycast(eventData, list);
            return list.Count > 0;
        }
        
        bool IsMouseOnUI()
        {
            Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            return IsPointerOverUIObject(mousePos);
        }

        bool IsTouchOnUI()
        {
            bool bRes = false;
            return bRes;
        }
    }
}